package com.zheng.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by likun on 2017/12/29.
 */
public class CamelUnderlineConverter {
    public static String camel2underline(String param){
        Pattern  p= Pattern.compile("[A-Z]");
        if(param==null ||param.equals("")){
            return "";
        }
        StringBuilder builder=new StringBuilder(param);
        Matcher mc=p.matcher(param);
        int i=0;
        while(mc.find()){
            builder.replace(mc.start()+i, mc.end()+i, "_"+mc.group().toLowerCase());
            i++;
        }

        if('_' == builder.charAt(0)){
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }
    public static String underline2camel(String param){
        Pattern  p=Pattern.compile("_[a-z]");
        if(param==null ||param.equals("")){
            return "";
        }
        StringBuilder builder=new StringBuilder(param);
        Matcher mc=p.matcher(param.toLowerCase());
        int i = 0;
        while(mc.find()){
            builder.replace(mc.start()-i, mc.end()-i, mc.group(0).substring(1).toUpperCase());
            i++;
        }

        return builder.toString();
    }
    public static void main(String[] args) {
        System.out.println(camel2underline("sayHelloWorld"));
    }
}